<?php 
/**
 *   Joomdiet - administrator/components/views/editrecipe/tmpl/default.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') or die('Restricted access'); ?>
<?php jimport('joomla.html.pane'); ?>
<form action="index.php" method="post" name="adminForm">
<div id="col100">
    <table cellspacing="0" cellpadding="0" border="0" width="100%">
        <tr><td valign="top">
        <fieldset class="adminform">
            <legend><?php echo JText::_('COM_JOOMDIET_EDIT_LEGEND'); ?></legend>
            <input type="hidden" name="recipe_id" id="recipe_id" value="<?php echo $this->row->id; ?>"/>
            <table class="adminform">
                <tr>
                    <td width="100" align="right" class="key">
                        <label for="recipe_name"><?php echo JText::_('COM_JOOMDIET_RECIPE'); ?></label>
                    </td>
                    <td>
                        <input class="text_area" type="text" name="recipe_name" id="recipe_name" size="32" maxlength="140" value="<?php echo $this->row->name;?>" />
                    </td>
                </tr>
                <tr>
                    <td width="100" align="right" class="key">
                        <label for="recipe_description"><?php echo JText::_('COM_JOOMDIET_DESCRIPTION'); ?></label>
                    </td>
                    <td>
                        <?php // loading joomla editor
                            $editor =& JFactory::getEditor();
                            $params = array('smilies' => '0',
                                            'style' => '1',
                                            'layer' => '0',
                                            'table' => '0',
                                            'clear_entities' => '0',
                                            'toggle' => '0');
                            echo $editor->display('recipe_description', $this->row->description, '600', '400', '40', '20', false, $params);
                        ?>
                    </td>
                </tr>
            </table>
        </fieldset>
        </td><td rowspan="2" valign="top" width="320px" style="padding: 7px 0 0 5px;">
            <?php
                $pane =& JPane::getInstance('sliders');
                echo $pane->startPane('pane');
                echo $pane->startPanel(JText::_('COM_JOOMDIET_PARENT_CATEGORY'), 'panel1');
            ?>
            <select style="width: 95%;" name="recipe_parent_category" id="recipe_parent_category" size="20" class="required mcat">
                <option value="0" <?php if ($this->row->recipe_category_id == 0) echo 'selected="selected"'; ?>>Root</option>
                <?php loadCategories($this->categories, 0, $this->row->recipe_category_id); ?>
            </select>
            <?php echo $pane->endPanel(); ?>
            <?php echo $pane->startPanel(JText::_('COM_JOOMDIET_VARIABLE_SELECTOR'), 'panel2'); ?>
                <select style="width: 95%" id="foodselector" name="foodselector" size="20" class="required mcat">
                    <?php foreach ($this->foods as $food) { 
                        if (!is_added($food->name)) { ?>
                        <option value="<?php echo $food->id;?>">
                            <?php echo $food->name; ?>
                        </option>
                    <?php } } ?>
                </select>
                <div style="padding: 7px 2px 2px 5px; float:right; ">
                <a href="javascript:void();" onclick="javascript: submitbutton('addfood');" name="addfood" class="toolbar"><?php echo JText::_('COM_JOOMDIET_ADD_FOOD'); ?><a />
                </div>
            <?php echo $pane->endPanel(); ?>
            <?php echo $pane->endPane(); ?>
        </td></tr>
        <tr><td valign="top">
            <fieldset class="adminform">
                <legend class="foods"><?php echo JText::_('COM_JOOMDIET_FOODS_TITLE'); ?></legend>
                <div id="vartable">
                    <input type="hidden" name="foodlist_count" id="foodlist_count" value="<?php echo count($this->recipefood)?>" />
                    <table class="adminlist">
                        <thead>
                            <th width="5"><?php echo JText::_('ID'); ?></th>
                            <th><?php echo JText::_('COM_JOOMDIET_FOODS_NAME'); ?></th>
                            <th width="110"><?php echo JText::_('COM_JOOMDIET_FOODS_AMOUNT'); ?></th>
                            <th width="50"><?php echo JText::_('COM_JOOMDIET_FOODS_DELETE'); ?></th>
                        </thead>
                        <tbody id="foodlist">
                            <?php foreach ($this->recipefood as $food) { 
                                $food_obj = $this->foodmodel->getFoodById($food->id_food); ?>
                                <tr>
                                    <td><?php echo $food->id_food; ?></td>
                                    <td><?php echo $food_obj->name; ?></td>
                                    <td>
                                        <input type="text" id="food<?php echo $food->id_food; ?>" name="food<?php echo $food->id_food; ?>" size="15" value="<?php echo $food->amount; ?>" />
                                    </td>
                                    <td><a href="index.php?option=com_joomdiet&controller=recipe&task=deletefood&rid=<?php echo $this->row->id; ?>&fid=<?php echo $food->id_food; ?>"><?php echo JText::_('COM_JOOMDIET_DELETE'); ?></a></td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </fieldset>
        </td></tr>
    </table>
</div>

<input type="hidden" name="option" value="com_joomdiet" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="recipe" />

</form>

<?php function loadCategories($categories, $level, $selected) { ?>
    <?php foreach ($categories as $category): ?>
        <option value="<?php echo $category['data']->id; ?>"
            <?php if ($category['data']->id == $selected) { echo 'selected="selected"'; } ?>>
            <?php for($p=0; $p < $level; $p++) { echo "&nbsp;&nbsp;"; } ?>
            &nbsp;&nbsp;<?php echo $category['data']->name; ?>
        </option>
        <?php loadCategories($category['children'], $level+1, $selected); ?>
    <?php endforeach; ?>
<?php } ?>

<?php function is_added($food_variables, $variable_id) {
    foreach ($food_variables as $food_variable) {
        if ($food_variable->id_variable == $variable_id)
            return true;
    }
    return false;
} ?>
